package common

import (
	"context"
	"net"
	"net/http"
	"time"
)

func ImageExists(url string) bool {
	tr := &http.Transport{
		MaxIdleConns: 100,
		DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
			conn, err := net.DialTimeout(network, addr, time.Second*2)
			if err != nil {
				return nil, err
			}

			err = conn.SetDeadline(time.Now().Add(time.Second * 3))
			if err != nil {
				return nil, err
			}
			return conn, nil
		},
	}
	client := &http.Client{
		Transport: tr,
	}
	req, _ := http.NewRequest("GET", url, nil)
	resp, _ := client.Do(req)
	ct := resp.Header.Get("Content-Type")

	if ct == "image/jpeg" {
		return true
	}

	return false
}
